package com.fndsoft.logstorage.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.fndsoft.logstorage.component.colog.COLog;

import android.os.Environment;

public class FileUtils {
   private String SDPATH;
   
   public String getSDPATH() {
	   return SDPATH;
   }
   
   public FileUtils() {
	   SDPATH = Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator;
   }
   
   public void deleteFile(String path) {
	   File file = new File(SDPATH + path);
	   file.delete();
   }
   public File createFile(String path) {
	   File file = new File(SDPATH + path);
	   return file;
   }
   public File createSDFile(String fileName) throws IOException {
	   File file = new File(SDPATH + fileName) ;
	   file.createNewFile();
	   return file;
   }
   
   public File createSDDir(String dirName) {
	   File dir = new File(SDPATH + dirName);
	   dir.mkdir();
	   COLog.d("createDir");
	   return dir;
   }
   
   public boolean isFileExist(String fileName) {
	   File file = new File(SDPATH +fileName) ;
	   return file.exists();
   }
   
   //正常
   public File write2SDFromInput(String path,String fileName,InputStream input) {
	   File file = null;
	   OutputStream output = null;
	   BufferedReader br = new BufferedReader(new InputStreamReader(input));
	   try{
		   createSDDir(path);
		   file = createSDFile(path+ fileName) ;
		   output = new FileOutputStream(file);
		   Date d = new Date();
		   SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		   
		   String date = dateFormat.format(d);
		   String line = "";
		   System.out.println("riqi" + date);
		   while((line = br.readLine())!= null) {
			   if(line.indexOf("COLog") >= 0) {
				   line = date +"---"+ line;
				   output.write(StringToByte(line,"UTF-8"));
				   byte []newLine="\n".getBytes();  
		            //写入换行  
				   output.write(newLine);
			   }
			  // if(line.indexOf("")) {}
		   }
		   output.flush();
		   output.close();
	   }catch(Exception e) {
		   e.printStackTrace();
	   }
	   return file;
   }
   
   //追加
   public void write2SDFromInput2(String path,InputStream input) {
	   File file = null;
	   FileWriter fw = null;
	   BufferedReader br = new BufferedReader(new InputStreamReader(input));
	   try{
		  
		   //file = createSDFile(path) ;
		   file = createFile(path);
		   fw = new FileWriter(file , true);
		   Date d = new Date();
		   SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		   
		   String date = dateFormat.format(d);
		   String line = "";
		   System.out.println("riqi" + date);
		   while((line = br.readLine())!= null) {
			   if(line.indexOf("COLog") >= 0) {
				   line = date +"---"+ line;
				   fw.write(line);
				   fw.write("\n");
			   }
			   if(line.indexOf("System.err") >= 0) {
				   line = date +"---"+ line;
				   fw.write(line);
				   fw.write("\n");
			   }
			  // if(line.indexOf("")) {}
		   }
		   fw.flush();
		   fw.close();
	   }catch(Exception e) {
		   e.printStackTrace();
	   }
   }
   
   //创建并覆盖
   public File write2SDFromInput2(String path,String fileName,InputStream input) {
	   File file = null;
	   OutputStream output = null;
	   BufferedReader br = new BufferedReader(new InputStreamReader(input));
	   try{
		   createSDDir(path);
		   file = createSDFile(path + fileName) ;
		   //file = createFile(path);
		   output = new FileOutputStream(file);
		   Date d = new Date();
		   SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		   
		   String date = dateFormat.format(d);
		   String line = "";
		   System.out.println("riqi" + date);
		   while((line = br.readLine())!= null) {
			   if(line.indexOf("COLog") >= 0) {
				   line = date +"---"+ line;
				   output.write(StringToByte(line,"UTF-8"));
				   byte []newLine="\n".getBytes();  
		            //写入换行  
				   output.write(newLine);
			   }
			   if(line.indexOf("System.err") >= 0) {
				   line = date +"---"+ line;
				   output.write(StringToByte(line,"UTF-8"));
				   byte []newLine="\n".getBytes();  
		            //写入换行  
				   output.write(newLine);
			   }
			  // if(line.indexOf("")) {}
		   }
		   output.flush();
		   output.close();
	   }catch(Exception e) {
		   e.printStackTrace();
	   }
	   return file;
   }
   
   
   /**
   * UTF-8 一个汉字占三个字节
   * @param str 源字符串
   * 转换成字节数组的字符串
   * @return
   */
   public static byte[] StringToByte(String str,String charEncode) {
	   byte[] destObj = null;
	   try {
	   if(null == str || str.trim().equals("")){ 
		   destObj = new byte[0]; 
		   return destObj; 
	   }else{ 
		   destObj = str.getBytes(charEncode);
	   }
	   } catch (UnsupportedEncodingException e) {
		   e.printStackTrace();
	   }
	   return destObj;
	 }
   
   public String getDateString() {
	   Date d = new Date();
	   SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	   
	   String date = dateFormat.format(d);
	   return date;
   }
   
}
